package com.util;

import cn.hutool.core.util.ArrayUtil;
import net.dongliu.apk.parser.ApkFile;
import net.dongliu.apk.parser.bean.ApkMeta;
import net.dongliu.apk.parser.bean.UseFeature;
import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbMakerConfigException;
import org.lionsoul.ip2region.DbSearcher;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ApkUtil {

    public static Map<String, Object> readAPK(String apkUrl) {
        Map<String, Object> resMap = new HashMap<String, Object>();
        try (ApkFile apkFile = new ApkFile(apkUrl)) {
            ApkMeta apkMeta = apkFile.getApkMeta();
            resMap.put("filename", apkMeta.getName());
            resMap.put("pkgname", apkMeta.getPackageName());
            resMap.put("versioncode", apkMeta.getVersionCode());
            resMap.put("versionname", apkMeta.getVersionName());
            for (UseFeature feature : apkMeta.getUsesFeatures()) {
                System.out.println(feature.getName());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resMap;

    }

    public static void main(String[] args) throws DbMakerConfigException, IOException {
        DbConfig config = new DbConfig() ;
        DbSearcher searcher = new DbSearcher(config , "C:\\ip2region.db") ;
        DataBlock dataBlock = searcher.btreeSearch("60.205.221.89") ;
        String[] arr = dataBlock.getRegion().split("\\|") ;
        if(arr[0].equals("中国") && !arr[2].equals("香港") && !arr[2].equals("澳门") && !arr[2].equals("台湾")) {
            // 国内处理
            System.out.println("国内");
        }else{
            // 国外处理
            System.out.println("国外");
        }
    }
}
